package com.gaobug.platform.service.impl;

import com.alibaba.nacos.common.utils.JacksonUtils;
import com.gaobug.base.cache.constant.CaptchaCode;
import com.gaobug.base.utils.email.MailSendInfo;
import com.gaobug.base.utils.mq.RabbitProducer;
import com.gaobug.base.utils.redis.RedisUtil;
import com.gaobug.platform.service.CaptchaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 博客分类接口
 */
@Slf4j
@RestController
@RequestMapping("/captcha")
@RefreshScope
public class CaptchaServiceImpl implements CaptchaService {
    @Value("#{rabbitMqFactory.queueConfigMap['email'].queueName}")
    private String queueName;
    @Value("#{rabbitMqFactory.queueConfigMap['email'].exchangeName}")
    private String exchangeName;
    @Value("${gaobug.captcha.email}")
    private String captchaHtml;
    @Resource
    private RabbitProducer rabbitProducer;

    /**
     * 获取博客列表
     */
    public void sendEmail(String email) {
        String verificationCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
        //邮件设置
        MailSendInfo mailSendInfo = new MailSendInfo();
        mailSendInfo.setSubject("电子邮箱验证码");
        mailSendInfo.setReceiver(email);
        mailSendInfo.setContext(captchaHtml.replace("{captcha}",verificationCode));
        String json = JacksonUtils.toJson(mailSendInfo);
        log.info(json);
        rabbitProducer.sendCompany(json, exchangeName, queueName);
        // 十分钟过期
        RedisUtil.set(CaptchaCode.CAPTCHA_EMAIL_REGISTER + email, verificationCode, 60_0_000L);
    }
}
